{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "AlphaPoseSample.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "gWsQmc-KEkY2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import drive\n",
        "import os\n",
        "\n",
        "# Googleドライブマウント\n",
        "drive.mount('/gdrive')\n",
        "\n",
        "# 起点ディレクトリ（半角英数のみで指定してください）\n",
        "base_path = \"/gdrive/My Drive/autotrace\"\n",
        "\n",
        "! echo \"autotraceフォルダの中身 -----------\"\n",
        "! ls -l \"$base_path\"\n",
        "! echo \"--------------------\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b8yXSUaxA2jU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# clone\n",
        "! git clone  --depth 1 https://github.com/miu200521358/AlphaPose-vmd.git"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DdqqiFY5PsOS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! mkdir -p ./AlphaPose-vmd/detector/yolo/data\n",
        "\n",
        "# yolov3-spp.weightsのダウンロード(YOLOモデル)\n",
        "file_id = \"1D47msNOOiJKvPOXlnpyzdKA3k6E97NTC\"\n",
        "file_name = \"yolov3-spp.weights\"\n",
        "! cd  ./AlphaPose-vmd/detector/yolo/data && curl -sc ./cookie \"https://drive.google.com/uc?export=download&id=$file_id\" > /dev/null\n",
        "code = \"$(awk '/_warning_/ {print $NF}' ./cookie)\"  \n",
        "! cd  ./AlphaPose-vmd/detector/yolo/data && curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=$code&id=$file_id\" -o \"$file_name\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1lckuzwfP8BC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! mkdir -p ./AlphaPose-vmd/detector/tracker/data\n",
        "\n",
        "# jde.uncertainty.ptのダウンロード(Trackerモデル)\n",
        "file_id = \"1nlnuYfGNuHWZztQHXwVZSL_FvfE551pA\"\n",
        "file_name = \"jde.1088x608.uncertainty.pt\"\n",
        "! cd  ./AlphaPose-vmd/detector/tracker/data && curl -sc ./cookie \"https://drive.google.com/uc?export=download&id=$file_id\" > /dev/null\n",
        "code = \"$(awk '/_warning_/ {print $NF}' ./cookie)\"  \n",
        "! cd  ./AlphaPose-vmd/detector/tracker/data && curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=$code&id=$file_id\" -o \"$file_name\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JspiIkz3Bs49",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! mkdir -p ./AlphaPose-vmd/pretrained_models\n",
        "\n",
        "# 学習済みデータのダウンロード(Simple Baseline)\n",
        "file_id = \"1nxyfUbvWDaaT9eDM7Y31ScSVPlGy6gfw\"\n",
        "file_name = \"simple_res50_256x192.pth\"\n",
        "! cd  ./AlphaPose-vmd/pretrained_models && curl -sc ./cookie \"https://drive.google.com/uc?export=download&id=$file_id\" > /dev/null\n",
        "code = \"$(awk '/_warning_/ {print $NF}' ./cookie)\"  \n",
        "! cd  ./AlphaPose-vmd/pretrained_models && curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=$code&id=$file_id\" -o \"$file_name\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fMhrTDl1e1Nk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 学習済みデータのダウンロード(Fast Pose)\n",
        "file_id = \"1kQhnMRURFiy7NsdS8EFL-8vtqEXOgECn\"\n",
        "file_name = \"fast_res50_256x192.pth\"\n",
        "! cd  ./AlphaPose-vmd/pretrained_models && curl -sc ./cookie \"https://drive.google.com/uc?export=download&id=$file_id\" > /dev/null\n",
        "code = \"$(awk '/_warning_/ {print $NF}' ./cookie)\"  \n",
        "! cd  ./AlphaPose-vmd/pretrained_models && curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=$code&id=$file_id\" -o \"$file_name\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sIa7KaThRRD6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# AlphaPose のライブラリインストール・ビルド\n",
        "# https://github.com/MVIG-SJTU/AlphaPose/issues/492\n",
        "! cd ./AlphaPose-vmd && python ./setup.py build develop"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "evTM_MiGVSG4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# ライブラリのアップデート\n",
        "\n",
        "# YAML（最新版でないとFullLoaderが使えない）\n",
        "# https://stackoverflow.com/questions/55551191/module-yaml-has-no-attribute-fullloader\n",
        "pip install -U PyYAML"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "U_-m2eTsMfb7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! cd AlphaPose-vmd && git pull"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ub4KOohJETvW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import datetime\n",
        "\n",
        "# 処理日時\n",
        "now_str = \"{0:%Y%m%d_%H%M%S}\".format(datetime.datetime.now())\n",
        "\n",
        "# 入力動画ファイル（半角英数のみで指定してください）\n",
        "input_name = \"input\"\n",
        "input_video = \"{0}/{1}.mp4\".format(base_path, input_name)\n",
        "\n",
        "# 出力先（サブディレクトリを生成します）\n",
        "outdir_path = \"{0}/output_{1}_{2}\".format(base_path, input_name, now_str)\n",
        "! mkdir \"$outdir_path\"\n",
        "\n",
        "# Simple Baseline\n",
        "cfg_path = \"configs/coco/resnet/256x192_res50_lr1e-3_1x-simple.yaml\"\n",
        "ckp_path = \"pretrained_models/simple_res50_256x192.pth\"\n",
        "\n",
        "# Fast Pose\n",
        "#cfg_path = \"configs/coco/resnet/256x192_res50_lr1e-3_1x.yaml\"\n",
        "#ckp_path = \"pretrained_models/fast_res50_256x192.pth\"\n",
        "\n",
        "# --format open: Openposeライクな出力形式\n",
        "# --detector \"tracker\": 追跡ありの検出方法\n",
        "# --detbatch 4: バッチサイズ\n",
        "# --save_video: 解析結果を動画で保存する\n",
        "! cd AlphaPose-vmd && python scripts/demo_inference.py --cfg \"$cfg_path\" --checkpoint \"$ckp_path\" --video \"$input_video\" --outdir \"$outdir_path\" --detector \"tracker\" --format \"open\" --detbatch 1 --save_video"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}